#include "cutil.h"
#include "wsecondspaceapp.h"
#include "ui_wsecondspaceapp.h"

/**
    @file wsecondspaceapp.cpp
    @author Claus Wimmer
    @date 2014-01-04

    Project: secondspace
    Licence: LGPL (Lesser GPL)
*/

// Parent widget not necessary.
WSecondSpaceApp::WSecondSpaceApp(QWidget *parent) : QMainWindow(parent), _stepcount(0), ui(new Ui::WSecondSpaceApp), _space(this) {
    // Gui.
    ui->setupUi(this);
    ui->bstart->setDefaultAction(ui->actionStart);
    ui->brewind->setDefaultAction(ui->actionRewind);
    ui->horizontalSlider->setMinimum(CUtil::minslider);
    ui->horizontalSlider->setMaximum(CUtil::maxslider);
    ui->horizontalSlider->setValue(CUtil::valslider);
    switch(CUtil::modus) {
    case CUtil::MODUS_0 : ui->rx->setChecked(true); break;
    case CUtil::MODUS_1 : ui->ry->setChecked(true); break;
    case CUtil::MODUS_2 : ui->rz->setChecked(true); break;
    }

    // Own.
    this->setGeometry(CUtil::x, CUtil::y, CUtil::width, CUtil::height);
    this->setWindowTitle(QString("%1, V %2").arg(CUtil::scenetitle).arg(CUtil::version));
    this->_timer.setInterval(CUtil::dt);
    this->registerSliderValueChanged(ui->horizontalSlider->value());
    this->showText(INITMSG);
    // Own signals.
    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(registerSliderValueChanged(int)));
    connect(ui->actionStart, SIGNAL(triggered()), this, SLOT(start()));
    connect(ui->actionStop, SIGNAL(triggered()), this, SLOT(stop()));
    connect(ui->actionRewind, SIGNAL(triggered()), this, SLOT(rewind()));
    connect(&_timer, SIGNAL(timeout()), this, SLOT(timeout()));
    connect(this, SIGNAL(initSpace()), &_space, SLOT(initSpace()));
    connect(this, SIGNAL(stepSpace()), &_space,SLOT(stepSpace()));
    connect(this, SIGNAL(sliderValueChanged(int)), &_space, SLOT(sliderValueChanged(int)));
    // Member signals.
    connect(&_space, SIGNAL(imageReady()), ui->scrollAreaWidgetContents, SLOT(imageReady()));
    connect(ui->scrollAreaWidgetContents, SIGNAL(mousePointed(uint,uint)), &_space, SLOT(mousePointed(uint,uint)));
    connect(ui->scrollAreaWidgetContents, SIGNAL(mousePointed(uint,uint)), this, SLOT(updateStatusText()));
    connect(ui->scrollAreaWidgetContents,SIGNAL(timeout()), this, SLOT(timeout()));
    // Qss Stylesheet.
    this->setStyleSheet(CUtil::qss());
    QDir output(CUtil::configdir + "/" + CUtil::outputdir);
    // Init dialog if screen shot files from previous exec have to be deleted.
    if(2 < output.count() && output.exists() && !CUtil::outputdir.isEmpty()){
        QMessageBox box(
            QMessageBox::Warning,
            "Secondspace - Save Video Image Batch",
            "Image 0utput Directory is not empty. After closing this message -> all files in this dir. will be deleted.",
            QMessageBox::Ok,
            this
        );
        // Qss of the popup.
        box.setStyleSheet(CUtil::qss());
        box.exec();
        // Delete the old image sequence.
        CUtil::clearImageDirectory();
    }
}

WSecondSpaceApp::~WSecondSpaceApp() {
    delete ui;
}

void WSecondSpaceApp::start() {
    // In case of prog start - make sure execution of the init kernel.
    if(0 == _stepcount){
        rewind();
    }
    // Start only if max number of steps is not reached.
    if(CUtil::stepcount > _stepcount){
        ui->bstart->removeAction(ui->actionStart);
        // Alternate the button image.
        ui->bstart->setDefaultAction(ui->actionStop);
        // Repeating timer triggering (step kernel + show image).
        _timer.start();
    }
}

void WSecondSpaceApp::stop() {
    // Alternate the button image.
    ui->bstart->removeAction(ui->actionStop);
    ui->bstart->setDefaultAction(ui->actionStart);
    // Switch off the timer triggering.
    _timer.stop();
}

void WSecondSpaceApp::rewind() {
    // Reset the counter.
    _stepcount = 0;
    // Show on statusbar.
    showText(INITMSG);
    // Take application init pict, show space excitation pict.
    ui->scrollAreaWidgetContents->setImage(_space.image());
    // Start the init kernel.
    emit initSpace();
}

void WSecondSpaceApp::timeout() {
    // Increase counter.
    _stepcount += CUtil::numsteps;
    // Exec step kernel.
    emit stepSpace();
    // Check if the end of the simulation is reached.
    if(CUtil::stepcount <= _stepcount){
        showText("Finish.");
        // Show on the statusbar.
        CUtil::showMessage(QString("Maximum number of steps reached : %1. See also configfile!").arg(CUtil::stepcount));
        // Switch off the timer.
        stop();
    } else {
        // Show on the statusbar.
        showText(QString("T[%1/%2]").arg(_stepcount).arg(CUtil::stepcount));
    }
}

void WSecondSpaceApp::registerSliderValueChanged(int value) {
    // Show the value of the slider control as its tooltip.
    ui->horizontalSlider->setToolTip(QString("[%1<=%2<=%3]")
         .arg(ui->horizontalSlider->minimum())
         .arg(ui->horizontalSlider->value())
         .arg(ui->horizontalSlider->maximum()));
    // Notify zspace.h and the kernels.
    emit sliderValueChanged(value);
}

// Show on the statusbar.
void WSecondSpaceApp::showText(QString msg) {
    QPoint p = ui->scrollAreaWidgetContents->mousePos();
    QString m = QString(", P[%1,%2]").arg(p.x() / CUtil::zoom).arg(p.y() / CUtil::zoom);
    ui->label->setText(msg + m);
}

void WSecondSpaceApp::updateStatusText() {
    showText("... ");
}

void WSecondSpaceApp::modusRadioButtonChecked() {

    if(ui->rx->isChecked()) {
        CUtil::modus = CUtil::MODUS_0;
    }

    if(ui->ry->isChecked()) {
        CUtil::modus = CUtil::MODUS_1;
    }

    if(ui->rz->isChecked()) {
        CUtil::modus = CUtil::MODUS_2;
    }
}
